package rmi.attempt1.net;

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

import java.util.ArrayList;
import java.util.List;

public class EventHandlerImpl extends UnicastRemoteObject implements EventHandler {
	private List<ChatEventListener> chatEventListener = new ArrayList<ChatEventListener>();

	public EventHandlerImpl() throws RemoteException {
		super();
	}

    public void addChatEventListener(ChatEventListener l) throws java.rmi.RemoteException {
		this.chatEventListener.add(l);
		System.out.println("<EventHandlerImpl> added ChatEventListener");
	}
    public void removeChatEventListener(ChatEventListener l) throws java.rmi.RemoteException {
		this.chatEventListener.remove(l);
		System.out.println("<EventHandlerImpl> removed ChatEventListener");
	}
    public void dispatchChatMessage(String origin, String message) throws java.rmi.RemoteException {
		System.out.println("<EventHandlerImpl> dispatching message: ["+origin+"] ["+message+"]");
		for (ChatEventListener l : this.chatEventListener) {
			l.chatMessage(new ChatEvent(origin, message));
		}
		System.out.println("<EventHandlerImpl> done dispatching message from ["+origin+"]");
	}
}
